
package com.tenentia.pcmps.swm.util.pdf;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.business.BusinessLogicObjectsFactory;
import com.tenentia.pcmps.swm.business.interfaces.PDFTemplatesManager;
import com.tenentia.pcmps.swm.data.beans.PDFTemplate;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class PDFGeneratorFactory {

	private static final Logger LOGGER = Logger.getLogger(PDFGeneratorFactory.class.getPackage().getName());

	private PDFGeneratorFactory() {

		super();
	}

	public static PDFGenerator getInstance(String templateAlias) throws Exception {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {

			PDFTemplatesManager templatesManager = (PDFTemplatesManager) BusinessLogicObjectsFactory.getInstance().createNewBusinessObject(BusinessLogicObjectsFactory.PDF_TEMPLATES_MANAGER);
			PDFTemplate template = templatesManager.getPDFTemplateByAlias(templateAlias);
			
			PDFGeneratorClassLoader pdfGeneratorClassLoader = new PDFGeneratorClassLoader(template.getClassData(), PDFGeneratorFactory.class.getClassLoader());			
			Class<?> tmpClass = pdfGeneratorClassLoader.loadClass(template.getClassName());
			PDFGenerator pdfGenerator = (PDFGenerator) tmpClass.newInstance();

			return pdfGenerator;
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new Exception("No se ha podido obtener la instancia de PDFGenerator.");
		}
	}
}
